package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.BaseCapacityRule;
import com.sailfish.springbootdemo.pojo.db1.LineBaseSmallType;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface BaseCapacityRuleDao {

    /**
     * 查询所有的
     *
     * @param searchValue
     * @return
     */
    @Results(id = "baseCapacityRuleResultMap", value = {
            @Result(column = "base_capacity_rule_id", property = "baseCapacityRuleId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_base_param_key", property = "lineBaseParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_param_keys", property = "pcbaDeviceParamKeys", jdbcType = JdbcType.VARCHAR),
            @Result(column = "base_capacity_rule_type", property = "baseCapacityRuleType", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "base_capacity_rule_name", property = "baseCapacityRuleName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "line_base_big_type_id", property = "lineBaseBigTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_base_small_type_id", property = "lineBaseSmallTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "device_use", property = "deviceUse", jdbcType = JdbcType.VARCHAR),
            @Result(column = "value_from_online", property = "valueFromOnline", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_keys2", property = "pcbaDeviceParamKeys2", jdbcType = JdbcType.VARCHAR),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.BASE_CAPACITY_RULE + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and (base_capacity_rule_name like CONCAT('%',#{searchValue},'%') or line_base_param_key like CONCAT('%',#{searchValue},'%'))" +
            "</if>" +
            "<if test='lineBaseBigTypeId != null and lineBaseBigTypeId != \"\" '>" +
            " and line_base_big_type_id = #{lineBaseBigTypeId}" +
            "</if>" +
            "<if test='lineBaseSmallTypeId != null and lineBaseSmallTypeId != \"\" '>" +
            " and line_base_small_type_id = #{lineBaseSmallTypeId}" +
            "</if>" +
            " order by sort" +
            "</script>")
    List<BaseCapacityRule> getAllBaseCapacityRule(@Param("searchValue") String searchValue, @Param("lineBaseBigTypeId") Integer lineBaseBigTypeId, @Param("lineBaseSmallTypeId") Integer lineBaseSmallTypeId);

    @ResultMap(value = "baseCapacityRuleResultMap")
    @Select("select * from " + DB1TableName.BASE_CAPACITY_RULE + " where base_capacity_rule_name = #{baseCapacityRuleName} and line_base_big_type_id = #{lineBaseBigTypeId} and line_base_small_type_id = #{lineBaseSmallTypeId}")
    List<BaseCapacityRule> getBaseCapacityRuleByTypeAndName(@Param("baseCapacityRuleName") String baseCapacityRuleName, @Param("lineBaseBigTypeId") Integer lineBaseBigTypeId, @Param("lineBaseSmallTypeId") Integer lineBaseSmallTypeId);

    /**
     * 根据key查询规则
     *
     * @param lineBaseParamKey
     * @return
     */
    @ResultMap(value = "baseCapacityRuleResultMap")
    @Select("select * from " + DB1TableName.BASE_CAPACITY_RULE + " where line_base_param_key = #{lineBaseParamKey}")
    BaseCapacityRule getBaseCapacityRuleByKey(@Param("lineBaseParamKey") String lineBaseParamKey);

    /**
     * 根据id查询
     *
     * @param baseCapacityRuleId
     * @return
     */
    @ResultMap(value = "baseCapacityRuleResultMap")
    @Select("select * from " + DB1TableName.BASE_CAPACITY_RULE + " where base_capacity_rule_id = #{baseCapacityRuleId}")
    BaseCapacityRule getBaseCapacityRuleById(@Param("baseCapacityRuleId") Integer baseCapacityRuleId);

    /**
     * 判断有没有重复的
     *
     * @return
     */
    @ResultMap(value = "baseCapacityRuleResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.BASE_CAPACITY_RULE + " where base_capacity_rule_name = #{baseCapacityRuleName} and line_base_big_type_id = #{lineBaseBigTypeId} and line_base_small_type_id = #{lineBaseSmallTypeId} " +
            "<if test='baseCapacityRuleId != null and baseCapacityRuleId != \"\" '>" +
            " and base_capacity_rule_id != #{baseCapacityRuleId}" +
            "</if>" +
            "</script>")
    List<BaseCapacityRule> judgeUniqueBaseCapacityRule(@Param("lineBaseBigTypeId") Integer lineBaseBigTypeId, @Param("lineBaseSmallTypeId") Integer lineBaseSmallTypeId, @Param("baseCapacityRuleId") Integer baseCapacityRuleId, @Param("baseCapacityRuleName") String baseCapacityRuleName);


    /**
     * 新增
     *
     * @param baseCapacityRule
     * @return
     */
    @Insert("insert into " + DB1TableName.BASE_CAPACITY_RULE + "(line_base_param_key,pcba_device_param_keys2,pcba_device_param_keys,base_capacity_rule_type,create_time,sort,status,base_capacity_rule_name,line_base_big_type_id,line_base_small_type_id,value_from_online) values " +
            "(#{baseCapacityRule.lineBaseParamKey},#{baseCapacityRule.pcbaDeviceParamKeys2},#{baseCapacityRule.pcbaDeviceParamKeys},#{baseCapacityRule.baseCapacityRuleType},#{baseCapacityRule.createTime},#{baseCapacityRule.sort},#{baseCapacityRule.status},#{baseCapacityRule.baseCapacityRuleName},#{baseCapacityRule.lineBaseBigTypeId},#{baseCapacityRule.lineBaseSmallTypeId},#{baseCapacityRule.valueFromOnline})")
    Integer addBaseCapacityRule(@Param("baseCapacityRule") BaseCapacityRule baseCapacityRule);

    /**
     * 删除
     *
     * @param baseCapacityRuleId
     * @return
     */
    @Delete("delete from " + DB1TableName.BASE_CAPACITY_RULE + " where base_capacity_rule_id = #{baseCapacityRuleId}")
    Integer deleteBaseCapacityRuleById(@Param("baseCapacityRuleId") Integer baseCapacityRuleId);

    /**
     * 更新
     *
     * @param baseCapacityRule
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.BASE_CAPACITY_RULE +
            "<set>" +
            "<if test='baseCapacityRule.lineBaseParamKey != null and baseCapacityRule.lineBaseParamKey != \"\" '>" +
            " line_base_param_key = #{baseCapacityRule.lineBaseParamKey}," +
            "</if>" +
            "<if test='baseCapacityRule.pcbaDeviceParamKeys != null and baseCapacityRule.pcbaDeviceParamKeys != \"\" '>" +
            " pcba_device_param_keys = #{baseCapacityRule.pcbaDeviceParamKeys}," +
            "</if>" +
            "<if test='baseCapacityRule.pcbaDeviceParamKeys2 != null and baseCapacityRule.pcbaDeviceParamKeys2 != \"\" '>" +
            " pcba_device_param_keys2 = #{baseCapacityRule.pcbaDeviceParamKeys2}," +
            "</if>" +
            "<if test='baseCapacityRule.baseCapacityRuleType != null and baseCapacityRule.baseCapacityRuleType != \"\" '>" +
            " base_capacity_rule_type = #{baseCapacityRule.baseCapacityRuleType}," +
            "</if>" +
            "<if test='baseCapacityRule.sort != null and baseCapacityRule.sort != \"\" '>" +
            " sort = #{baseCapacityRule.sort}," +
            "</if>" +
            "<if test='baseCapacityRule.status != null and baseCapacityRule.status != \"\" '>" +
            " status = #{baseCapacityRule.status}," +
            "</if>" +
            "<if test='baseCapacityRule.baseCapacityRuleName != null and baseCapacityRule.baseCapacityRuleName != \"\" '>" +
            " base_capacity_rule_name = #{baseCapacityRule.baseCapacityRuleName}," +
            "</if>" +
            "<if test='baseCapacityRule.updateTime != null '>" +
            " update_time = #{baseCapacityRule.updateTime}," +
            "</if>" +
            "<if test='baseCapacityRule.lineBaseBigTypeId != null and baseCapacityRule.lineBaseBigTypeId != \"\" '>" +
            " line_base_big_type_id = #{baseCapacityRule.lineBaseBigTypeId}," +
            "</if>" +
            "<if test='baseCapacityRule.lineBaseSmallTypeId != null and baseCapacityRule.lineBaseSmallTypeId != \"\" '>" +
            " line_base_small_type_id = #{baseCapacityRule.lineBaseSmallTypeId}," +
            "</if>" +
            "<if test='baseCapacityRule.valueFromOnline != null and baseCapacityRule.valueFromOnline != \"\" '>" +
            " value_from_online = #{baseCapacityRule.valueFromOnline}," +
            "</if>" +
            "</set>" +
            " where base_capacity_rule_id = #{baseCapacityRule.baseCapacityRuleId}" +
            "</script>")
    Integer updateBaseCapacityRule(@Param("baseCapacityRule") BaseCapacityRule baseCapacityRule);
}
